import numpy as np
import networkx as nx

on_server = False

def load_prot_prot():
    """
    Parses the protein protein interaction dataset
    
    Returns
    -------
    :type G: networkx
    :param G: networkx G graph object
    
    :type F: numpy 2D array
    :param F: nodes features matrix
    """
    
    local_path = '/Users/rockyrock/Documents/University/Konstanz/Lectures/'+\
        'Master thesis/Datasets/MatrixFact/Protein-protein interaction data/ext/'
    server_path = '/home/rdirbas/datasets/prot-prot/'#always append with a /
    path = ''
    
    if on_server:
        path = server_path
    else:
        path = local_path
        
    # read the first line to determine the number of columns    
    with open(path+'Adj', 'rb') as f:
        ncols = len(next(f).split('\t'))
        
    x = np.genfromtxt(path+'Adj', delimiter='\t', dtype=None, names=True,
                  usecols=range(1,ncols) # skip the first column
                  )
    labels = x.dtype.names
    
    y = x.view(dtype=('int', len(x.dtype)))# y is a view of x
    G = nx.from_numpy_matrix(y, create_using=nx.DiGraph())
    G = nx.relabel_nodes(G, dict(zip(range(ncols-1), labels)))
    
    #Now read the node features matrix
    with open(path+'Atts', 'rb') as f:
        ncols = len(next(f).split('\t'))
    
    x = np.genfromtxt(path+'Atts', delimiter='\t', dtype=None, names=True,
                  usecols=range(1,ncols) # skip the first column
                  )
    Nodes_X = x.view(dtype=('int', len(x.dtype)))
    
    return G, Nodes_X
    
def main():
    print 'hi'
    
    G, Nodes_X = load_prot_prot()
    
    print G.number_of_nodes(), G.number_of_edges()
    print Nodes_X.shape
    
    
if __name__ == "__main__":
    main()
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    